package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.EmployeeQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping("list")
    @ResponseBody
    public List<Department> list() {
        return departmentService.selectAll();
    }

    @RequestMapping("view")
    @RequiresPermissions(value = {"department:view","部门界面"},logical = Logical.OR)
    public String query(QueryObject qo) {
        return "department";
    }

    @RequestMapping("saveOrUpdate")
    @RequiresPermissions(value = {"department:saveOrUpdate","部门新增/更新"},logical = Logical.OR)
    @ResponseBody
    public Object saveOrUpdate(Department department) {
        JSONResult result = new JSONResult();
        try {
            departmentService.saveOrUpdate(department);
        } catch (Exception e) {
            result.remark(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("validateName")
    @ResponseBody
    public Boolean validateName(String sn, Long id) {
        return departmentService.validateName(sn, id);
    }

    @RequestMapping("query")
    @ResponseBody
    public PageResult department(QueryObject qo) {
        return departmentService.query(qo);
    }

    @RequestMapping("changeState")
    @RequiresPermissions(value = {"department:changeState","部门状态"},logical = Logical.OR)
    @ResponseBody
    public JSONResult changeState(Long id) {
        JSONResult result = new JSONResult();
        try {
            departmentService.changeState(id);
        } catch (Exception e) {
            result.remark(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
}
